<%@ page language="java" import="cn.hutool.core.date.DateUnit" pageEncoding="UTF-8" %>
<%@ page import="cn.hutool.core.date.DateUtil" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.OnLineMonitor" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.systeminfo.SysMaintenanceLog" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%--验证移动短信验证码登录系统--%>
<%
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server
%>
<%
    String message = "";
    String loginid = Util.null2String(request.getParameter("loginid"));
    String yzm = Util.null2String(request.getParameter("yzm"));
    Map<String, Object> apidatas = new HashMap<String, Object>();
    RecordSet rs = new RecordSet();
    rs.writeLog(">>>>>loginid=" + loginid + ",yzm=" + yzm);
    rs.executeQuery("select * from hrmresource where loginid=?", loginid);
    if (rs.next()) {
        String id = Util.null2String(rs.getString("id"));
        String result = isValidVerifyCode(yzm, id);
        if ("验证码正确".equals(result)) {
            User user = new User();
            user.setUid(Util.getIntValue(id));
            user.setLoginid(rs.getString("loginid"));
            user.setFirstname(rs.getString("firstname"));
            String lastname = rs.getString("lastname");
            user.setLastname(lastname);
            user.setAliasname(rs.getString("aliasname"));
            user.setTitle(rs.getString("title"));
            user.setTitlelocation(rs.getString("titlelocation"));
            user.setSex(rs.getString("sex"));
            user.setPwd(rs.getString("password"));
            String languageidweaver = rs.getString("systemlanguage");
            user.setLanguage(Util.getIntValue(languageidweaver, 0));

            user.setTelephone(rs.getString("telephone"));
            user.setMobile(rs.getString("mobile"));
            user.setMobilecall(rs.getString("mobilecall"));
            user.setEmail(rs.getString("email"));
            user.setCountryid(rs.getString("countryid"));
            user.setLocationid(rs.getString("locationid"));
            user.setResourcetype(rs.getString("resourcetype"));
            user.setStartdate(rs.getString("startdate"));
            user.setEnddate(rs.getString("enddate"));
            user.setContractdate(rs.getString("contractdate"));
            user.setJobtitle(rs.getString("jobtitle"));
            user.setJobgroup(rs.getString("jobgroup"));
            user.setJobactivity(rs.getString("jobactivity"));
            user.setJoblevel(rs.getString("joblevel"));
            user.setSeclevel(rs.getString("seclevel"));
            user.setUserDepartment(Util.getIntValue(rs.getString("departmentid"), 0));
            user.setUserSubCompany1(Util.getIntValue(rs.getString("subcompanyid1"), 0));
            user.setUserSubCompany2(Util.getIntValue(rs.getString("subcompanyid2"), 0));
            user.setUserSubCompany3(Util.getIntValue(rs.getString("subcompanyid3"), 0));
            user.setUserSubCompany4(Util.getIntValue(rs.getString("subcompanyid4"), 0));
            user.setManagerid(rs.getString("managerid"));
            user.setAssistantid(rs.getString("assistantid"));
            user.setPurchaselimit(rs.getString("purchaselimit"));
            user.setCurrencyid(rs.getString("currencyid"));
            user.setLastlogindate(rs.getString("currentdate"));
            user.setLogintype("1");
            user.setAccount(rs.getString("account"));

            user.setLoginip(request.getRemoteAddr());
            request.getSession(true).setMaxInactiveInterval(60 * 60 * 24);
            request.getSession(true).setAttribute("weaver_user@bean", user);
            request.getSession(true).setAttribute("moniter", new OnLineMonitor("" + user.getUID(), user.getLoginip()));
            Util.setCookie(response, "loginfileweaver", "/main.jsp", 172800);
            Util.setCookie(response, "loginidweaver", "" + user.getUID(), 172800);
            Util.setCookie(response, "languageidweaver", languageidweaver, 172800);
            //用户的登录后的页面
            weaver.systeminfo.template.UserTemplate ut = new weaver.systeminfo.template.UserTemplate();
            ut.getTemplateByUID(user.getUID(), user.getUserSubCompany1());
            int templateId = ut.getTemplateId();
            int extendTempletid = ut.getExtendtempletid();
            int extendtempletvalueid = ut.getExtendtempletvalueid();
            String defaultHp = ut.getDefaultHp();
            session.setAttribute("defaultHp", defaultHp);
            Map logmessages = (Map) application.getAttribute("logmessages");
            if (logmessages == null) {
                logmessages = new HashMap();
                logmessages.put("" + user.getUID(), "");
                application.setAttribute("logmessages", logmessages);
            }
            session.setAttribute("logmessage", "");
            recordLogin(Util.getIntValue(id), lastname, request.getRemoteAddr());
            message = "success";
        } else {
            message = result;
        }
    } else {
        message = "无效用户名";
    }
    apidatas.put("message", message);
    out.print(JSONObject.toJSONString(apidatas));
%>
<%!
    public static long getDiffMinute(String begintime, String endtime) {
        Date data1 = DateUtil.parse(begintime);
        Date date2 = DateUtil.parse(endtime);
        return DateUtil.between(data1, date2, DateUnit.SECOND);
    }

    public static String isValidVerifyCode(String yzm, String id) {
        RecordSet rs = new RecordSet();
        String message = "验证码错误";
        String fullDate = weaver.common.DateUtil.getFullDate();
        String sql = "select field129 from cus_fielddata where scopeid='-1' and scope='HrmCustomFieldByInfoType' and field127=? and id=?";
        rs.executeQuery(sql, yzm, id);
        if (rs.next()) {
            String yzmyxsj = Util.null2String(rs.getString("field129"));
            rs.writeLog(">>>>>yzmyxsj=" + yzmyxsj + ",fullDate=" + fullDate);
            long diffmin = getDiffMinute(yzmyxsj, fullDate);
            if (diffmin > 600) {
                message = "验证码已过期";
            } else {
                message = "验证码正确";
            }
        }
        return message;
    }

    /**
     * 记录登录日志
     *
     * @param hrmResourceId 人员ID
     * @param lastname      人员姓名
     * @param ipAddress     IP地址
     */
    public static void recordLogin(int hrmResourceId, String lastname, String ipAddress) {
        try {
            /*记录登入日志*/
            SysMaintenanceLog sysMaintenanceLog = new SysMaintenanceLog();
            sysMaintenanceLog.resetParameter();
            sysMaintenanceLog.setRelatedId(hrmResourceId);
            sysMaintenanceLog.setRelatedName(lastname);
            sysMaintenanceLog.setOperateType("6");
            sysMaintenanceLog.setOperateDesc("");
            sysMaintenanceLog.setOperateItem("60");
            sysMaintenanceLog.setOperateUserid(hrmResourceId);
            sysMaintenanceLog.setClientAddress(ipAddress);
            sysMaintenanceLog.setSysLogInfo();
            /*记录登入日志*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

%>



